package com.sfac.springboot.modules.test.controller;

import com.sfac.springboot.modules.common.vo.Result;
import com.sfac.springboot.modules.common.vo.Search;
import com.sfac.springboot.modules.test.entity.Student;
import com.sfac.springboot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jzq
 * @date 2022/8/19 15:08
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/api")
public class StudentController {

    @Autowired
    private StudentService studentService;


    /**
     * localhost/api/student    ---- post
     * {"studentName":"张三"}
     * {"studentName":"李四","studentCard":"{"cardNum":"2022050511"}"}
     *
     * @param student
     * @return
     */
    @PostMapping(value = "/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }


    /**
     * localhost/api/student    ---- put
     *
     * @param student
     * @return
     */
    @PutMapping("/student")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }


    /**
     * localhost/api/student/11    ---- delete
     *
     * @param id
     * @return
     */
    @DeleteMapping("/student/{id}")
    public Result<Object> deleteStudentById(@PathVariable int id) {
        return studentService.deleteStudentById(id);
    }


    /**
     * localhost/api/student/2    ---- get
     *
     * @param id
     * @return
     */
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable int id) {
        return studentService.getStudentById(id);
    }


    /**
     * localhost/api/students    ---- post
     * {"currentPage":"1","pageSize":"5","sort":"studentName","direction":"desc","keyword":""}
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/students", consumes = "application/json")
    public Page getStudentsBySearch(@RequestBody Search search) {
        return studentService.getStudentsBySearch(search);
    }


    /**
     * localhost/api/students?studentName=    ---- get
     *
     * @param id
     * @return
     */
    @GetMapping("/students")
    public List<Student> getStudentsByName(@RequestParam String studentName) {
        return studentService.getStudentsByName(studentName);
    }


}
